package com.chushouya.manager.dao.repository;

import com.chushouya.manager.dao.entity.SuggestEntity;
import com.chushouya.manager.dao.mapper.SuggestEntityMapper;
import com.chushouya.manager.dto.admin.suggest.SuggestQuery;
import com.general.framework.core.lang.Strings;
import com.general.framework.core.schema.PageResult;
import com.general.framework.data.mybatis.BaseRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.Objects;

/**
 * 反馈建议Repository
 */
@Repository
@Slf4j
public class SuggestRepository extends BaseRepository<SuggestEntityMapper, SuggestEntity, Long> {

    /**
     * 查询单个反馈
     */
    public SuggestEntity selectOne(SuggestQuery query) {
        return this.selectFirstOneByExample(this.buildExample(query));
    }

    /**
     * 分页查询反馈列表
     */
    public PageResult<SuggestEntity> selectPageResult(SuggestQuery query) {
        final Example example = this.buildExample(query);
        example.orderBy("createTime").desc();
        return selectPage(query.getPageRequest(), example);
    }

    /**
     * 保存反馈
     */
    public void saveSuggest(SuggestEntity suggest) {
        this.insertSelective(suggest);
    }

    /**
     * 更新反馈
     */
    public void updateSuggest(SuggestEntity suggest) {
        this.updateByPrimaryKeySelective(suggest);
    }

    /**
     * 构建查询条件
     */
    private Example buildExample(SuggestQuery query) {
        Example example = this.buildExample();
        
        if (Objects.nonNull(query.getSuggestId())) {
            example.and().andEqualTo("suggestId", query.getSuggestId());
        }
        
        if (Objects.nonNull(query.getSuggestType())) {
            example.and().andEqualTo("suggestType", query.getSuggestType());
        }
        
        if (Strings.isNotEmpty(query.getSuggestContent())) {
            example.and().andLike("suggestContent", Strings.format("%{}%", query.getSuggestContent()));
        }
        
        if (Objects.nonNull(query.getUserId())) {
            example.and().andEqualTo("userId", query.getUserId());
        }
        
        if (Strings.isNotEmpty(query.getUserName())) {
            example.and().andLike("userName", Strings.format("%{}%", query.getUserName()));
        }
        
        if (Strings.isNotEmpty(query.getUserContact())) {
            example.and().andEqualTo("userContact", query.getUserContact());
        }
        
        if (Objects.nonNull(query.getUserType())) {
            example.and().andEqualTo("userType", query.getUserType());
        }
        
        if (Strings.isNotEmpty(query.getHandleUser())) {
            example.and().andLike("handleUser", Strings.format("%{}%", query.getHandleUser()));
        }
        
        if (Objects.nonNull(query.getHandleStatus())) {
            example.and().andEqualTo("handleStatus", query.getHandleStatus());
        }
        
        if (Strings.isNotEmpty(query.getOrderNo())) {
            example.and().andEqualTo("orderNo", query.getOrderNo());
        }
        
        if (Objects.nonNull(query.getOrderId())) {
            example.and().andEqualTo("orderId", query.getOrderId());
        }
        
        if (Objects.nonNull(query.getCreateTimeStart()) && Objects.nonNull(query.getCreateTimeEnd())) {
            example.and().andBetween("createTime", query.getCreateTimeStart(), query.getCreateTimeEnd());
        }
        
        return example;
    }
}
